package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.fdb.basic.framework.core.constant.PubConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.fdb.efp.console.service.facade.CoeffCfgService;
import com.fdb.efp.console.service.vo.CoeffCfgVO;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;

/**
 * <p>
 * 提额系数配置表的Controller类
 * </p>
 * @author szbd
 * @createDate 2018-09-17 15:10:09
 */
@RestController
@RequestMapping("/api")
public class CoeffCfgController extends AbstractController {

    private static Logger logger = LoggerFactory.getLogger(CoeffCfgController.class);

    @Autowired
    @Qualifier("coeffCfgService")
    private CoeffCfgService coeffCfgService;
    
    @Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
    /**
     * 列表数据查询
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value="/prd/coeff/cfgs", method=RequestMethod.POST)
    public @ResponseBody ResponseData<List<CoeffCfgVO>> queryCoeffCfgAll(@RequestBody CoeffCfgVO vo) {
        logger.info("查询提额系数配置表的列表数据，入参：" + JSONObject.toJSONString(vo));
		// 根据用户角色获取用户权限方案
		List<CoeffCfgVO> outputVo = null;
        Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
        setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = coeffCfgService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = coeffCfgService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = coeffCfgService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = coeffCfgService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
    }
    
    /**
     * 根据主键查询详情
     * @return
     */
    @RequestMapping(value="/coeff/cfg/{typeId}{legalOrgCode}", method=RequestMethod.GET)
    public @ResponseBody ResponseData<CoeffCfgVO> queryCoeffCfgByPk(@PathVariable("typeId") String typeId, @PathVariable("legalOrgCode") String legalOrgCode) {
        CoeffCfgVO inVo = new CoeffCfgVO();
        inVo.setTypeId(typeId);//类型代码
        inVo.setLegalOrgCode(legalOrgCode);//机构法人代码
        logger.info("根据主键查询提额系数配置表的详情，入参：" + JSONObject.toJSONString(inVo));
        CoeffCfgVO outputVo = coeffCfgService.queryCoeffCfgByPk(inVo);
        return getResponseData(outputVo);
    }
    
    /**
     * 根据主键删除信息
     * @param inCoeffCfgVo
     * @return
     */
    @RequestMapping(value="/coeff/cfg", method=RequestMethod.DELETE)
    public @ResponseBody ResponseData<Integer> deleteCoeffCfgByPk(@RequestBody CoeffCfgVO inCoeffCfgVo) {
        logger.info("根据主键删除提额系数配置表信息，入参：" + JSONObject.toJSONString(inCoeffCfgVo));
        int outputVo = coeffCfgService.deleteCoeffCfgByPk(inCoeffCfgVo);
        logger.info("删除提额系数配置表影响记录数：" + outputVo);
        return getResponseData(outputVo);
    }
    
    /**
     * 根据主键更新信息
     * @param inCoeffCfgVo
     * @return
     */
    @RequestMapping(value="/coeff/cfg", method=RequestMethod.PUT)
    public @ResponseBody ResponseData<Integer> updateCoeffCfgByPk(@RequestBody CoeffCfgVO inCoeffCfgVo) {
        logger.info("根据主键更新提额系数配置表信息，入参：" + JSONObject.toJSONString(inCoeffCfgVo));
        int outputVo = coeffCfgService.updateCoeffCfgByPk(inCoeffCfgVo);
        logger.info("更新提额系数配置表影响记录数：" + outputVo);
        return getResponseData(outputVo);
    }
    
    /**
     * 新增数据
     * @param inCoeffCfgVo
     * @return
     */
    @RequestMapping(value="/coeff/cfg", method=RequestMethod.POST)
    public @ResponseBody ResponseData<Integer> insertCoeffCfg(@RequestBody CoeffCfgVO inCoeffCfgVo) {
        logger.info("新增提额系数配置表的记录，入参：" + JSONObject.toJSONString(inCoeffCfgVo));
        int outputVo = coeffCfgService.insertCoeffCfg(inCoeffCfgVo);
        logger.info("新增提额系数配置表影响记录数：" + outputVo);
        return getResponseData(outputVo);
    }
}